/**
 * 
 */
package com.vladimirjosu.fhc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import com.vladimirjosu.util.Util;

/**
 * @author Vladimir
 * 
 */
public class CardGame extends Problem implements Comparator<Long> {

	private static final long MOD = 1000000007;
	private ArrayList<Long> a;
	private Long n;
	private Long k;

	public CardGame(String first, String second) {
		super();
		String[] split = first.split(" ");
		n = Long.valueOf(split[0]);
		k = Long.valueOf(split[1]);
		String[] strings = second.split(" ");
		a = new ArrayList<Long>();
		for (int i = 0; i < strings.length; i++) {
			a.add(i, Long.valueOf(strings[i]));
		}
	}

	public String solve() {
		Collections.sort(a, this);
		int index = 0;
		long sum = 0;
		long x = Util.comb(k, n, MOD);
		long y = Util.comb(k - 1, n - (index + 1), MOD);
		while (x > y) {
			sum = sum + (a.get(index) * y);
			x = x - y;
			index++;
			y = Util.comb(k - 1, n - (index + 1), MOD);
		}
		sum = sum + (a.get(index) * x);
		sum = sum % MOD;
		return String.valueOf(sum);
	}

	@Override
	public int compare(Long o1, Long o2) {
		return o1 < o2 ? 1 : -1;
	}
}
